﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows.Input;
using VisualWip.Core;
using VisualWip.Core.Columns;
using VisualWip.Core.Interfaces;

namespace VisualWip.PresentationModel.WorkItem
{
    public class WorkItemViewModel
    {
        private readonly WorkItemsColumnBase _column;
        private readonly WorkItemSize _size;
        private readonly Core.WorkItem _workItem;
        private ReadOnlyCollection<WorkItemViewModel> _children;
        private ICommand _openCommand;

        public WorkItemViewModel(Core.WorkItem workItem, WorkItemsColumnBase column)
            : this(workItem, column, column.WorkItemSize)
        {
        }

        private WorkItemViewModel(Core.WorkItem workItem, WorkItemsColumnBase column, WorkItemSize size)
        {
            _workItem = workItem;
            _size = size;
            _column = column;
            Attach(workItem.Children, column.SizeStratergy.NextSize(size));
            SetupWorkItemBehaviors(workItem);
        }


        public ReadOnlyCollection<WorkItemViewModel> Children
        {
            get { return _children; }
        }

        public string TypeName
        {
            get { return _workItem.TypeName; }
        }

        public string Id
        {
            get { return _workItem.Id; }
        }

        public virtual string Title
        {
            get { return _workItem.Title; }
        }

        public virtual string Heading
        {
            get
            {
                return IsIdAndTitleEmpty()
                           ? string.Empty
                           : string.Format(CultureInfo.CurrentCulture, "{0}-{1}", _workItem.Id, _workItem.Title);
            }
        }

        public virtual string State
        {
            get { return _workItem.State; }
        }

        public virtual Dictionary<string, object> Fields
        {
            get { return _workItem.Fields; }
        }

        public string Size
        {
            get { return _size.Name; }
        }

        public ICommand OpenCommand
        {
            get { return _openCommand; }
        }

        private void SetupWorkItemBehaviors(Core.WorkItem workItem)
        {
            foreach (IWorkItemBehavior workItemBehavior in workItem.Behaviors)
            {
                var x = workItemBehavior.Provider as IWorkItemBehaviorToCommandProvider;
                if (x != null)
                {
                    if (workItemBehavior is OpenWorkItemBehavior)
                    {
                        _openCommand = x.GetCommand(workItemBehavior);
                    }
                }
            }
        }

        private void Attach(IEnumerable<Core.WorkItem> children, WorkItemSize size)
        {
            var result = new List<WorkItemViewModel>();
            foreach (Core.WorkItem workItem in children)
            {
                var wivm = new WorkItemViewModel(workItem, _column, size);
                result.Add(wivm);
            }

            _children = new ReadOnlyCollection<WorkItemViewModel>(result);
        }


        private bool IsIdAndTitleEmpty()
        {
            return string.IsNullOrWhiteSpace(_workItem.Id) && string.IsNullOrWhiteSpace(_workItem.Title);
        }
    }
}